package com.generic;

import java.util.ArrayList;

/**
 * @author Vhong
 * @version 1.0
 */
public class Generic02 {
    public static void main(String[] args) {
        //1.当我们ArrayList<Dog>表示存放到ArrayList 集合中的元素是Dog类型
        //2.如果编译器发现添加的类型，不满足要求，就会报错
        //3.在遍历的时候，可以直接取出Dog类型而不是Object
        //4.public class ArrayList<E>{} E称为泛型，那么Dog->E
        ArrayList<Dog1> arrayList = new ArrayList<Dog1>();

        arrayList.add(new Dog1("旺财",10));
        arrayList.add(new Dog1("发财",1));
        arrayList.add(new Dog1("小黄",5));
        System.out.println("==========使用泛型================");
        for (Dog1 dog : arrayList)
        {
            System.out.println(dog.getName()+" - "+dog.getAge());
        }
        System.out.println();
        //arrayList.add(new Cat("招财猫",8));
    }
}
class Dog1{
    private String name;
    private int age;

    public Dog1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
class Cat1{
    private String name;
    private int age;

    public Cat1(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
